<template>
	<view class="education">
		<view class="flex alignCenter tip">
			<image src="https://image.alhelp.net/uploads/20231104/f5a50463b8dd7a1b1ea25f211c8ffd65.png" mode="widthFix"></image>
			<view class="font14 color999">看到对方的之前，需要您填写自己的教育信息喔！</view>
		</view>
		<view class="">
			<radio-group @change="radioChange" class="flex alignCenter justConAround">
				<label class="uni-list-cell uni-list-cell-pd flex alignCenter font14" v-for="(item, index) in eduArr" :key="index">
					<view>
						<radio :value="item.value" style="transform:scale(0.7)" color="#F7DD5E" :checked="index == current-1" />
					</view>
					<view>{{item.title}}</view>
				</label>
			</radio-group>
		</view>
		<view class="year">
			<uni-mdlist :listData="itemArr" @clickItem="clickItem"></uni-mdlist>
		</view>
		<view class="schoolAndMajor">
			<uni-mdlist :listData="otherArr" @clickItem="clickItem"></uni-mdlist>
		</view>
		<view class="cardItem">
			<view class="font14 flex alignCenter"><text class="colorRed">*</text><text>证件证明</text></view>
			<view class="font14 upload">
				<view class="flex alignCenter">
					<uni-singleimg :fileUrl="fileUrl" v-if="fileUrl" @delPic="delPic"></uni-singleimg>
					<view class="bold uploadCommon colorccc" @click="uploadFileClick">+</view>
				</view>
				<view class="color999 normalTip">在校生可上传学生证,刚录取的可上传录取通知书,已毕业的可上传毕业证、结业证等</view>
				<view class="colorMain importantTip">认证后会获得标识别，更容易得到信任喔！</view>
			</view>
		</view>
		<!-- <view class="cardItem">
			<view class="font14 flex alignCenter"><text>简介</text></view>
			<view class="font14">
				<textarea class="introduction font14" maxlength="100" placeholder="简介内容(100个字符以内)" v-model.trim="introduction" cols="30" rows="10"></textarea>
			</view>
		</view> -->
		<view class="fixedBottomBtn" v-if="(!showYear&&!showProvince&&!showCollege)" @click="sureClick">确定</view>
		<!-- 选择年份 -->
		<uni-yearpicker :showYear="showYear" @cancel="cancel" @sureYear="sureYear"></uni-yearpicker>
		<!-- 选择学校 -->
		<uni-chooseschool v-if="showProvince" :showProvince="showProvince" @schoolSuccess="schoolSuccess" @cancelProvince="cancelProvince"></uni-chooseschool>
		<!-- 选择专业 -->
		<uni-choosemajor v-if="showCollege" :school="otherArr[0].school" :showCollege="showCollege" @majorSuccess="majorSuccess" @cancelColl="cancelColl"></uni-choosemajor>
	</view>
</template>

<script>
	import { addEducation,eduDetail,editEdu } from "@/api/mine.js"
	import { uploadFile,chooseImg,previewImg} from "@/utils/common.js"
	import uniYearpicker from "@/settingPages/pages/components/uni-yearpicker/uni-yearpicker.vue"
	import uniChoosemajor from "@/settingPages/pages/components/uni-choosemajor/uni-choosemajor.vue"
	export default {
		components:{
			uniYearpicker,
			uniChoosemajor
		},
		data() {
			return {
				current:2,
				showYear:false,
				showProvince:false,
				showCollege:false,
				eduArr:[{
					title:"本科",
					value:'1',
				},{
					title:"硕士",
					value:'2'
				},{
					title:"博士",
					value:'3'
				}],
				itemArr:[{
					title:'入学年份',
					icon:'/static/image/setting/cal.png',
					val:''
				}],
				otherArr:[{
					title:'大学',
					icon:'/static/image/setting/major.png',
					school:'',
					val:''
				},{
					title:'专业',
					icon:'/static/image/setting/major.png',
					college:'',
					major:'',
					val:''
				}],
				imageValue:[],
				fileUrl:'',
				fromPage:'',
				editId:'',
				introduction:'',
				eduInfo:''
			};
		},
		onLoad(option) {
			this.fromPage=option.from?option.from:''
			this.editId=option.id?option.id:''
			if(this.editId){
				this.getDetail()
			}
		},
		methods:{
			radioChange(e){
				// console.log(e)
				this.this.introduction=''
				this.current=e.detail.value
			},
			// 详情（编辑时调用）
			getDetail(){
				eduDetail({id:this.editId}).then((res)=>{
					if(res.code==100000){
						this.eduInfo=res.data
						// 数据回显
						this.current=res.data.diploma
						this.itemArr[0].val=res.data.graduationYear
						this.otherArr[0].school={
							title:res.data.schoolName,
							schoolCode:res.data.schoolCode
						}
						this.otherArr[0].val=res.data.schoolName
						this.otherArr[1].major={
							title:res.data.majorName,
							majorCode:res.data.majorCode
						}
						this.otherArr[1].val=res.data.majorName
						this.fileUrl=res.data.imgUrl
					}
				})
			},
			// 列表功能的点击事件
			clickItem(item){
				if(item.title=='入学年份'){
					this.showYear=true
					return
				}else if(item.title=='大学'){
					this.showProvince=true
					return
				}else if(item.title=='专业'){
					console.log('专业',this.otherArr[0])
					if(!this.otherArr[0].school){
						uni.showToast({
							icon:'none',
							title:'请先选择学校'
						})
						return
					}
					this.showCollege=true
					return
				}
			},
			uploadFileClick(){
				chooseImg(1).then((files)=>{
					uploadFile(files).then((successFile)=>{
						console.log('文件',successFile)
						this.fileUrl=successFile.url
					})
				})
			},
			// 预览图片
			showBigPic(){
				previewImg([this.fileUrl])
			},
			delPic(){
				this.fileUrl=''
			},
			cancel(){
				this.showYear=false
			},
			sureYear(year){
				this.itemArr[0].val=year
				this.showYear=false
			},
			cancelProvince(){
				this.showProvince=false
			},
			cancelColl(){
				this.showCollege=false
			},
			// 选择学校后的确定回调事件
			schoolSuccess(item){
				this.showProvince=false
				this.otherArr.forEach((val)=>{
					if(val.title=='大学'){
						val.val=item.school.title
						val.school=item.school
					}else if(val.title=='专业'){
						val.val=''
						val.college=''
						val.major=''
					}
				})
			},
			majorSuccess(item){
				this.showCollege=false
				this.otherArr.forEach((val)=>{
					if(val.title=='专业'){
						val.val=item.major.title
						val.college=item.college
						val.major=item.major
					}
				})
			},
			sureClick(){
				let canNext=true
				this.itemArr.forEach((item)=>{
					if(!item.val){
						uni.showToast({
							title:`请选择${item.title}`,
							icon:'none'
						})
						canNext=false
						return
					}
				})
				this.otherArr.forEach((item)=>{
					if(!item.val){
						uni.showToast({
							title:`请选择${item.title}`,
							icon:'none'
						})
						canNext=false
						return
					}
				})
				if(!this.fileUrl){
					uni.showToast({
						title:'请上传学生证/录取通知书/毕业证/结业证',
						icon:'none'
					})
					return
				}
				if(canNext){
					let param={
						diploma:this.current,//1本科；2硕士研究生（学长学姐卡片上显示的学历是这个）；3博士研究生
						graduationYear:this.itemArr[0].val,
						schoolCode:this.otherArr[0].school.schoolCode,
						schoolName:this.otherArr[0].school.title,
						majorCode:this.otherArr[1].major.majorCode,
						majorName:this.otherArr[1].major.title,
						imgUrl:this.fileUrl,
						introduce:this.introduction
					}
					if(this.editId){
						param.id=this.editId
						editEdu(param).then((res)=>{
							if(res.code==100000){
								console.error("传的",this.eduInfo)
								// Object.assign(this.$data, this.$options.data())
								uni.navigateTo({
									url:`/pages/send/lookFor/postgraduate/enrollment/advantage?backDelta=3&diploma=${this.current}`
								})
								// uni.navigateBack()
							}
						})
					}else{
						addEducation(param).then((res)=>{
							if(res.code==100000){
								Object.assign(this.$data, this.$options.data())
								// if(this.fromPage){
								// 	uni.navigateBack()
								// }else{
								// 	uni.navigateTo({
								// 		url:'/pages/setting/education/eduCardList/eduCardList'
								// 	})
								// }
								uni.navigateTo({
									url:`/pages/send/lookFor/postgraduate/enrollment/advantage?backDelta=3&diploma=${this.current}`
								})
								// uni.navigateBack()
							}
						})
					}
				}
			}
		}
	}
</script>
<style lang="scss">
page{
	background-color:$pageBgColor;
}
</style>
<style lang="scss" scoped>
.education{
	padding: 0 30rpx 120rpx;
	box-sizing: border-box;
	.tip{
		padding: 30rpx 80rpx;
		box-sizing: border-box;
		margin:0 0 60rpx 0;
		image{
			width:92rpx;
			height:92rpx;
			margin:0 20rpx 0 0;
		}
	}
}
.year{
	margin:30rpx 0;
}
.cardItem{
	background: #fff;
	border-radius: 10rpx;
	padding: 30rpx;
	box-sizing: border-box;
	margin: 30rpx 0;
	.upload{
		margin:30rpx 0 0;
	}
	.title{
		flex-shrink: 0;
	}
	input{
		width:100%;
		text-align: right;
	}
	textarea{
		margin:30rpx 0 0;
		padding: 15rpx;
		background: $pageBgColor;
		width: 100%;
		box-sizing: border-box;
		border-radius: 10rpx;
	}
}
.normalBtnStyle{
	margin: 72rpx auto 0;
}
</style>
